package org.bill.excel.converter.impl;

import java.lang.reflect.Type;
import java.util.Date;

import net.feimz.utils._DateUtil;

import org.bill.excel.converter.FieldValueConverter;
import org.bill.excel.vo.OutValue;

/**
 * 默认的处理方法
 * @author Bill
 * @create @2017年4月6日
 */
public class AbstractFieldConvertor implements FieldValueConverter {

	
	@Override
	public OutValue toExcel(Object source) {
		if(source==null){
			return null;
		}
		if (source instanceof Integer) {
			Integer value = ((Integer) source).intValue();
			return new OutValue(OutValue.OutType.DOUBLE, value.doubleValue());
		} else if (source instanceof String) {
			String value = (String) source;
			return new OutValue(OutValue.OutType.STRING, value);
		} else if (source instanceof Double) {
			double value = ((Double) source).doubleValue();
			return new OutValue(OutValue.OutType.DOUBLE, value);
		} else if (source instanceof Float) {
			Float value = ((Float) source).floatValue();
			return new OutValue(OutValue.OutType.DOUBLE, value.doubleValue());
		} else if (source instanceof Long) {
			Long value = ((Long) source).longValue();
			return new OutValue(OutValue.OutType.DOUBLE, value.doubleValue());
		} else if (source instanceof Boolean) {
			boolean value = ((Boolean) source).booleanValue();
			return new OutValue(OutValue.OutType.BOOLEAN, value);
		} else if (source instanceof Date) {
			Date value = (Date) source;
			//return new OutValue(OutValue.OutType.DATE, value);
			return new OutValue(OutValue.OutType.STRING, _DateUtil.format(value,"yyyy-MM-dd HH:mm:ss"));
		}
		
		return new OutValue(OutValue.OutType.STRING, source.toString());
	}

	@Override
	public Object toObject(String cell,Type targetType) {
		if (cell == null) {
			return null;
		}
		return cell;
	}

}
